package com.dgut.uhrs.controller;
import com.dgut.uhrs.bean.Key;
import com.dgut.uhrs.bean.User;
import com.dgut.uhrs.service.UserService;
import com.dgut.uhrs.util.JWTUtil;
import com.dgut.uhrs.vo.DataVO;
import com.dgut.uhrs.vo.UserVO;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

@Controller
public class UserController {

    @Autowired
    private UserService userService;

    @RequestMapping(value = "/admin/addUser")
    @ResponseBody
    public void addUser(User user){
        userService.addUser(user);
    }

    @RequestMapping(value = "/admin/editUser")
    @ResponseBody
    public void editUser(User user){
        userService.editUser(user);
    }




    @RequestMapping(value = "/user/userLoginByOpenId")
    @ResponseBody
    public User userLoginByOpenId(String openId){
        User user1 = userService.userLoginByOpenId(openId);
        if (user1!=null){
            user1.setToken(JWTUtil.createUserToken(user1));
            return user1;
        }
        else {
            return null;
        }
    }

    @RequestMapping(value = "/user/userCheck")
    @ResponseBody
    public User userCheck(User user){
        User user1 = userService.userCheck(user);
        if (user1!=null){
            return user1;
        }
        else {
            return null;
        }
    }
    @RequestMapping(value = "/user/lockWechat")
    @ResponseBody
    public Boolean lockWechat(User user){
        User user1 = userService.checkOpenId(user.getOpenId());
        if (user1!=null){
            return false;
        }else {
            userService.lockWechat(user);
            return true;
        }

    }

    @RequestMapping(value = "/user/userLogin")
    @ResponseBody
    public User userLogin(User user){
        User user1 = userService.userLogin(user);
        if (user1!=null){
                user1.setToken(JWTUtil.createUserToken(user1));
                return user1;
            }
            else {
                return null;
            }
    }

    @RequestMapping(value = "/admin/toNotClockPerson")
    public String toNotClickPerson(){
        return "notClockPerson";
    }
    @RequestMapping(value = "/admin/findNotClockPerson")
    @ResponseBody
    public DataVO<UserVO> findNotClockPerson(Key key, HttpServletRequest request){
        key = setKey(key,request);
        return userService.findNotClockPerson(key);
    }




    @RequestMapping(value = "/admin/toShowAllUser")
    public String toShowAllUser(){
        return "allUser";
    }
    @RequestMapping(value = "/admin/showAllUser")
    @ResponseBody
    public DataVO<UserVO> showAllUser(Key key, HttpServletRequest request){
        key = setKey(key,request);
        DataVO<UserVO> userVODataVO = userService.showAllUser(key);
        return userService.showAllUser(key);
    }

    @RequestMapping(value = "/admin/deleteUser")
    @ResponseBody
    public void deleteUser(String userId){
        userService.deleteUser(userId);
    }

    @RequestMapping(value = "/admin/changeStatus")
    @ResponseBody
    public void changeStatus(String userId, String status){
        if (status.equals("未锁定")){
            userService.lockUser(userId);
        }else {
            userService.unLockUser(userId);
        }
    }




    public Key setKey(Key key, HttpServletRequest request){
        key.setId(request.getParameter("key[id]"));
        key.setName(request.getParameter("key[name]"));
        key.setDate(request.getParameter("key[date]"));
        key.setAddress(request.getParameter("key[address]"));
        key.setClas(request.getParameter("key[clas]"));
        key.setLimit(Integer.parseInt(request.getParameter("limit")));
        key.setSchool(request.getParameter("key[school]"));
        key.setPhone(request.getParameter("key[phone]"));
        key.setPage((key.getPage()-1)* key.getLimit());
        key.setToken(request.getParameter("key[token]"));
        return key;
    }

}
